package com.yzt.zhuju.application;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.fragment.app.FragmentActivity;

import com.alibaba.android.arouter.launcher.ARouter;
import com.yzt.zhuju.R;
import com.yzt.zhuju.utils.ActivityManagerUtils;
import com.yzt.zhuju.utils.ViewUtils;
import com.yzt.plugin_widget.AppTitleBar;

/**
 * author : bob
 * date   : 2022/11/29
 * desc   :
 */
public abstract class BaseAct extends FragmentActivity implements View.OnClickListener {

    protected AppTitleBar mTitleBar;

    public LinearLayout mNormalLayout;

    public View viewTransparent;

    protected View mStatusBarLayout;

    protected FragmentActivity mActivity;

    protected View mLayoutPage;

    protected View mLayoutLoading;

    protected View mLayoutError;

    protected View mLayoutEmpty;

    protected ImageView ivLoadingAnim;

    protected AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
        mActivity = this;
        initBaseData();
        ActivityManagerUtils.getInstance().addActivity(this);
    }

    private void initBaseData() {
        super.setContentView(R.layout.base_act);
        mNormalLayout = findViewById(R.id.ll_normalView);
        viewTransparent = findViewById(R.id.view_transparent);
        mTitleBar = findViewById(R.id.tb_title);
        mStatusBarLayout = findViewById(R.id.statubar_layout);
        mTitleBar.setLeftBtnListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });
        mLayoutPage = findViewById(R.id.layout_status_page);
        mLayoutLoading = findViewById(R.id.layout_loading);
        mLayoutError = findViewById(R.id.layout_error);
        mLayoutEmpty = findViewById(R.id.layout_empty);

        mStatusBarLayout.getLayoutParams().height = ViewUtils.getStatusBarHeight(this);
    }

    @Override
    public void setContentView(int layoutResID) {
        LayoutInflater inflater = LayoutInflater.from(this);
        inflater.inflate(layoutResID, mNormalLayout);
    }

    /**
     * 正常页面
     */
    protected void showNormalView() {
        mLayoutPage.setVisibility(View.GONE);
        mNormalLayout.setVisibility(View.VISIBLE);
        if (animationDrawable != null) {
            animationDrawable.stop();
        }
    }

    /**
     * 加载中
     */
    protected void showLoadingView() {
        setDefaultStatus();
        animationDrawable = (AnimationDrawable) ivLoadingAnim.getBackground();
        if (animationDrawable != null) {
            animationDrawable.start();
        }
    }

    private void setDefaultStatus() {
        mLayoutPage.setVisibility(View.VISIBLE);
        mNormalLayout.setVisibility(View.GONE);
    }

    public void hideLayoutPage() {
        showNormalView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManagerUtils.getInstance().remove(this);
    }

    @Override
    public void onClick(View view) {
        onClick(view.getId());
    }

    protected void onClick(int clickId) {

    }

    protected void onRetryRequest() {

    }
}
